1
|
|
|
import { Inject } from '@nestjs/common'; |
2
|
|
|
import { CommandHandler } from '@nestjs/cqrs'; |
3
|
|
|
import { UserAdministrativeMissingException } from 'src/Domain/HumanResource/User/Exception/UserAdministrativeMissingException'; |
4
|
|
|
import { UserNotFoundException } from 'src/Domain/HumanResource/User/Exception/UserNotFoundException'; |
5
|
|
|
import { IUserAdministrativeRepository } from 'src/Domain/HumanResource/User/Repository/IUserAdministrativeRepository'; |
6
|
|
|
import { IUserRepository } from 'src/Domain/HumanResource/User/Repository/IUserRepository'; |
7
|
|
|
import { UpdateUserCommand } from './UpdateUserCommand'; |
8
|
|
|
|
9
|
|
|
@CommandHandler(UpdateUserCommand) |
10
|
|
|
export class UpdateUserCommandHandler { |
11
|
|
|
constructor( |
12
|
|
|
@Inject('IUserRepository') |
13
|
|
|
private readonly userRepository: IUserRepository, |
14
|
|
|
@Inject('IUserAdministrativeRepository') |
15
|
|
|
private readonly userAdministrativeRepository: IUserAdministrativeRepository |
16
|
|
|
) {} |
17
|
|
|
|
18
|
|
|
public async execute(command: UpdateUserCommand): Promise<void> { |
19
|
|
|
const { |
20
|
|
|
id, |
21
|
|
|
role, |
22
|
|
|
annualEarnings, |
23
|
|
|
contract, |
24
|
|
|
workingTime, |
25
|
|
|
executivePosition, |
26
|
|
|
healthInsurance, |
27
|
|
|
joiningDate, |
28
|
|
|
leavingDate, |
29
|
|
|
transportFee, |
30
|
|
|
sustainableMobilityFee |
31
|
|
|
} = command; |
32
|
|
|
|
33
|
|
|
const user = await this.userRepository.findOneById(id); |
34
|
|
|
if (!user) { |
35
|
|
|
throw new UserNotFoundException(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
const userAdministrative = await this.userAdministrativeRepository.findOneByUserId( |
39
|
|
|
id |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
if (!userAdministrative) { |
43
|
|
|
throw new UserAdministrativeMissingException(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
user.updateRole(role); |
47
|
|
|
userAdministrative.update( |
48
|
|
|
Math.round(annualEarnings * 100), |
49
|
|
|
contract, |
50
|
|
|
workingTime, |
51
|
|
|
executivePosition, |
52
|
|
|
healthInsurance, |
53
|
|
|
joiningDate, |
54
|
|
|
leavingDate ? leavingDate : null, |
55
|
|
|
transportFee ? Math.round(transportFee * 100) : 0, |
56
|
|
|
sustainableMobilityFee ? Math.round(sustainableMobilityFee * 100) : 0 |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
await this.userRepository.save(user); |
60
|
|
|
await this.userAdministrativeRepository.save(userAdministrative); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|